package com.nord.EventList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.android.maps.MapActivity;
import com.nord.R;
import com.nord.MapResources.GOOGLEOverlay;
import com.nord.MapResources.OSMOverlay;
import com.nord.R.id;
import com.nord.R.layout;
import com.nord.Registration.Registration;
import com.nord.Services.SQLiteAdapter;

public class Location extends MapActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location);
		Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		String location = getIntent().getExtras().getString("LOCATION");
		if(location != null){
			if(location.compareTo("[]") == 0){
				Intent selection = new Intent(Location.this, Registration.class);
				selection.putExtra("EVENTNAME", getIntent().getExtras().getString("EVENTNAME"));
				selection.putExtra("USERNAME", getIntent().getExtras().getString("USERNAME"));
				Toast toast = Toast.makeText(Location.this,
						"Fann ekki staðsetningu\nReyndu aftur síðar",
						Toast.LENGTH_LONG);
				toast.show();
				startActivity(selection);
			}
			else{
				double lng = Double.valueOf(location.substring(0,location.indexOf(":")));
				double lat = Double.valueOf(location.substring(location.indexOf(":") + 1, location.length()));
				Drawable marker = getResources().getDrawable(android.R.drawable.star_big_on);
				int markerWidth = marker.getIntrinsicWidth();
				int markerHeight = marker.getIntrinsicHeight();
				marker.setBounds(0, markerHeight, markerWidth, 0);
		
				if (isSDPresent) {
					org.osmdroid.views.MapView mapView = (org.osmdroid.views.MapView) findViewById(R.id.openMapviewLocation);
					org.osmdroid.views.MapController mapController = mapView
							.getController();
		
					mapView.setVisibility(View.VISIBLE);
					mapView.setTileSource(org.osmdroid.tileprovider.tilesource.TileSourceFactory.MAPNIK);
					mapView.setBuiltInZoomControls(true);
					mapController.setZoom(15);
		
					org.osmdroid.ResourceProxy resourceProxy = new org.osmdroid.DefaultResourceProxyImpl(
							getApplicationContext());
					OSMOverlay osmItemizedOverlay = new OSMOverlay(marker,
							resourceProxy);
					mapView.getOverlays().add(osmItemizedOverlay);
		
					org.osmdroid.util.GeoPoint gp = new org.osmdroid.util.GeoPoint(
							(int) (lng * 1E6), (int) (lat * 1E6));
					mapController.setCenter(gp);
					osmItemizedOverlay.addItem(gp, "Point", "Point");
				} else {
		
					com.google.android.maps.MapView mapView = (com.google.android.maps.MapView) findViewById(R.id.gMapviewLocation);
					com.google.android.maps.MapController mapController = mapView
							.getController();
		
					GOOGLEOverlay itemizedoverlay = new GOOGLEOverlay(marker);
					mapView.setVisibility(View.VISIBLE);
					mapView.setBuiltInZoomControls(true);
					mapController = mapView.getController();
					mapController.setZoom(15);
		
					com.google.android.maps.GeoPoint gp = new com.google.android.maps.GeoPoint(
							(int) (lng * 1E6), (int) (lat * 1E6));
					com.google.android.maps.OverlayItem overlayitem = new com.google.android.maps.OverlayItem(
							gp, "", "");
		
					mapController.setCenter(gp);
					itemizedoverlay.addOverlay(overlayitem);
					mapView.getOverlays().add(itemizedoverlay);
				
				}
			}
		}
		else{
			Intent selection = new Intent(Location.this, Registration.class);
			selection.putExtra("EVENTNAME", getIntent().getExtras().getString("EVENTNAME"));
			selection.putExtra("USERNAME", getIntent().getExtras().getString("USERNAME"));
			Toast toast = Toast.makeText(Location.this,
					"Fann ekki staðsetningu\nReyndu aftur síðar",
					Toast.LENGTH_LONG);
			toast.show();
			startActivity(selection);
		}
	}
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}